package com.doform.android.xianggucheng.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.doform.android.xianggucheng.BaseTimePickerActivity;
import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.bean.ReportBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqReport;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.widget.DividerItemDecoration;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * Created by Administrator on 2017/5/10.
 */

public class ReportActivity extends BaseTimePickerActivity {

    @BindView(R.id.ll)
    LinearLayout mLL;

    @BindView(R.id.tv_query_start)
    TextView mQueryStart;

    @BindView(R.id.tv_query_stop)
    TextView mQueryStop;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private CommonAdapter<ReportBean.ListBean> mCommonAdapter;

    private String sdate;
    private String edate;

    @Override
    protected void showView() {
        titleBar.setTextTitle("经营报表");
        titleBar.setRightText("查询");
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_report;
    }


    @Override
    protected void initView() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        long curTime = System.currentTimeMillis();
        long startTime = curTime - 7 * 24 * 60 * 60 * 1000;

        sdate = formatter.format(new Date(startTime));
        edate = formatter.format(new Date(curTime));

        mQueryStart.setText(sdate);
        mQueryStop.setText(edate);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
    }


    private List<ReportBean.ListBean> mDatas = new ArrayList<>();

    @Override
    protected void updateData() {
        super.updateData();
        getDataForService(sdate, edate);
    }

    private void beforeQuery() {
        String sdate = mQueryStart.getText().toString().trim();
        String edate = mQueryStop.getText().toString().trim();

        getDataForService(sdate, edate);
    }

    private void getDataForService(String sdate, String edate) {

        HttpManager
                .instance()
                .getReport(new ReqReport(sdate, edate))
                .subscribe(new ErrorHandleSubscriber<ReportBean>() {
                    @Override
                    public void onNext(@NonNull ReportBean reportBean) {
                        parseData(reportBean.getList());
                    }
                });


//                                    parseData(bean.getMess().getData());
    }

    private void parseData(List<ReportBean.ListBean> data) {
        mDatas.clear();
        mDatas.addAll(data);
        if (mCommonAdapter != null) {
            mCommonAdapter.notifyDataSetChanged();
            if (mLL.getVisibility() == View.VISIBLE) {
                mLL.setVisibility(View.GONE);
            }
        }

    }


    @Override
    protected void initListener() {

        initRecyclerAdapter();

        titleBar.setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mLL.getVisibility() == View.VISIBLE) {
                    mLL.setVisibility(View.GONE);
                } else {
                    mLL.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void initRecyclerAdapter() {

        mCommonAdapter = new CommonAdapter<ReportBean.ListBean>(this, R.layout.activity_report_item, mDatas) {
            @Override
            protected void convert(ViewHolder holder, ReportBean.ListBean listBean, int position) {
                TextView tv_date = holder.getView(R.id.tv_date);
                TextView tv_order_num = holder.getView(R.id.tv_order_num);
                TextView tv_sales = holder.getView(R.id.tv_sales);
                TextView tv_revenues = holder.getView(R.id.tv_revenues);

                tv_date.setText(listBean.getAdd_date());
                tv_order_num.setText(listBean.getNum());
                tv_sales.setText("￥" + listBean.getTotal());
                tv_revenues.setText("￥" + listBean.getHaspay());

            }
        };

        mRecyclerView.setAdapter(mCommonAdapter);

    }

    @OnClick({R.id.btn_query, R.id.tv_query_start, R.id.tv_query_stop})
    protected void processClick(View v) {
        switch (v.getId()) {
            case R.id.btn_query:
                beforeQuery();
                break;
            case R.id.tv_query_start:
                pvTime.show(mQueryStart);
                break;
            case R.id.tv_query_stop:
                pvTime2.show(mQueryStop);
                break;
            default:
                break;

        }
    }


}
